var utils = require('util'),
  fs = require('fs');

module.exports = function (grunt) {
  var mode  = grunt.option('mode'),
      ver   = grunt.option('ver'),
      debug = grunt.option('debug');
  
  
	var clean         = require('./clean.js'),
      copy          = require('./copy.js'),
      concat        = require('./concat.js'),
      uglify        = require('./uglify.js'),
      cssmin        = require('./cssmin.js'),
      //ejs           = require('./ejs.js'),
      jade          = require('./jade.js'),
      filerev       = require('./filerev.js'),
      watch         = require('./watch.js'),
      qunit         = require('./qunit.js'),
      connect       = require('./connect.js'),
      usemin        = require('./usemin.js'),
      useminPrepare = require('./useminPrepare.js'),
      htmlmin       = require('./htmlmin.js'),
      less          = require('./less.js'),
      autoprefixer  = require('./autoprefixer.js'),
      csscomb       = require('./csscomb.js'),
      requirejs     = require('./requirejs.js'),
      
      // 开发或发布
      modeMap       = require('./mode.js'),
      // 版本数据
      versionMap    = require('./versionSettings.js');
  
  mode = mode || 'dev';
  versions = ver? ver.split(',') : Object.keys(versionMap);
  
  useminPrepare   = useminPrepare.init  (mode);
  usemin          = usemin      .init(mode);
  clean           = clean       .init(mode);
  htmlmin         = htmlmin     .init(mode, versionMap, versions);
  copy            = copy        .init(grunt, versionMap, modeMap, mode, versions);
  cssmin          = cssmin      .init(grunt, versionMap, modeMap, mode, versions);
  filerev         = filerev     .init(grunt, versionMap, modeMap, mode, versions);
  uglify          = uglify      .init(grunt, versionMap, modeMap, mode, versions);
  //ejs             = ejs         .init(grunt, versionMap, modeMap, mode, versions);
  jade            = jade        .init(grunt, versionMap, modeMap, mode, versions);
  less            = less        .init(grunt, versionMap, modeMap, mode, versions);
  autoprefixer    = autoprefixer.init(grunt, versionMap, modeMap, mode, versions);
  csscomb         = csscomb     .init(grunt, versionMap, modeMap, mode, versions);
  requirejs       = requirejs   .init(grunt, versionMap, modeMap, mode, versions);
  
	// grunt配置
	grunt.initConfig({
    requirejs     : requirejs,
		clean         : clean,
		copy          : copy,
		concat        : concat,
		uglify        : uglify,
		cssmin        : cssmin,
		watch         : watch,
		//ejs           : ejs,
		filerev       : filerev,
		useminPrepare : useminPrepare,
		usemin        : usemin,
		connect       : connect,
    htmlmin       : htmlmin,
		qunit         : qunit,
		less          : less,
		autoprefixer  : autoprefixer,
		csscomb       : csscomb,
    jade          : jade,
    replace: {
      css: {
        src: ['dist/rls/m/index.html'],             // source files array (supports minimatch)
        overwrite: true,
        //dest: 'build/text/',             // destination directory or file
        replacements: [{
          from: /(css\/index\.min\.[\w\d]{8,8}\.css)/,                   // string replacement
          to: 'http://static.360buyimg.com/mr/<%=grunt.template.today("yyyy-m-d")%>/pad/$1'
        }]
      },
      js: {
        src: ['dist/rls/m/js/main.min.*.js'],
        overwrite: true,
        replacements: [{
          from: 'baseUrl:"js"',                   // string replacement
          to: 'baseUrl:"http://static.360buyimg.com/mr/<%=grunt.template.today("yyyy-m-d")%>/pad/js"'
        }]
      }
    },
		jshint        : {
			all : ['src/js/common.js']
		}
	});

	// 加载插件
	grunt.loadNpmTasks('grunt-contrib-concat');
	grunt.loadNpmTasks('grunt-contrib-uglify');
	grunt.loadNpmTasks('grunt-contrib-cssmin');
	grunt.loadNpmTasks('grunt-contrib-watch');
	grunt.loadNpmTasks('grunt-contrib-jshint');
	grunt.loadNpmTasks('grunt-contrib-copy');
	grunt.loadNpmTasks('grunt-contrib-clean');
	grunt.loadNpmTasks('grunt-contrib-connect');
	grunt.loadNpmTasks('grunt-contrib-qunit');
	//grunt.loadNpmTasks('grunt-ejs');
	grunt.loadNpmTasks('grunt-usemin');
	grunt.loadNpmTasks('grunt-filerev');
	grunt.loadNpmTasks('grunt-contrib-less');
	grunt.loadNpmTasks('grunt-contrib-htmlmin');
	grunt.loadNpmTasks('grunt-autoprefixer');
	grunt.loadNpmTasks('grunt-csscomb');
	grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-requirejs');
  grunt.loadNpmTasks('grunt-text-replace');
	
  var steps = [];
  steps.push('clean:build');
  if(0 == debug){
    steps.push('clean:temp');
    steps.push('useminPrepare');
  }
  steps.push('jade');  
  steps.push('copy');
  steps.push('less');
  steps.push('autoprefixer');
  if(0 != debug){
    steps.push('usemin');
  }
  if(0 == debug){
    steps.push('uglify');
    steps.push('cssmin');
    steps.push('filerev');
    steps.push('clean:js');
    steps.push('usemin');
    steps.push('htmlmin');
    steps.push('replace');
  }
  
  if(0 != debug){
    steps.push('connect');
    steps.push('watch');
  }
  
  console.log(steps.valueOf());
  grunt.registerTask('default', steps);
  grunt.registerTask('test', ["jade"]);
};